package s_processPkg

import (
	"net"
)

// 有且只有一个
// 全局变量
var (
	OnlineUserMgr *UserMgr
	HeartBeatMp   map[uint]chan bool
)

type UserMgr struct {
	OnlineUsers map[uint]*UserProcess
	UserTokens  map[uint]string
}

func init() {
	OnlineUserMgr = &UserMgr{
		OnlineUsers: make(map[uint]*UserProcess, 1024),
		UserTokens:  make(map[uint]string, 1024),
	}
	HeartBeatMp = make(map[uint]chan bool, 1024)
	for i := uint(0); i < 1024; i++ {
		HeartBeatMp[i] = make(chan bool, 1024)
	}
}

// 通过id获取到Conn
func GetConnById(id uint) net.Conn {
	if OnlineUserMgr.OnlineUsers[id] != nil {
		return OnlineUserMgr.OnlineUsers[id].Conn
	}
	return nil
}

// 通过Conn获取到UserId
func GetUserIdByConn(conn net.Conn) (userId uint, err error) {
	for k, v := range OnlineUserMgr.OnlineUsers {
		if v.Conn == conn {
			userId = k
			return
		}
	}
	return
}

func (this *UserMgr) AddOnlineUser(up *UserProcess) {
	this.OnlineUsers[up.UserId] = up
}
